package com.agency8x.controller;

import java.util.List;

/**
 * 提现管理
 */
import com.agency8x.bean.ResultInfoBean;
import com.agency8x.bean.SearchParamBean;
import com.agency8x.config.AppConf;
import com.agency8x.interceptor.CheckLoginInterceptor;
import com.agency8x.model.AdminModel;
import com.agency8x.model.AuthRuleModel;
import com.agency8x.model.LoginModel;
import com.agency8x.model.WithdrawModel;
import com.agency8x.validator.WithdrawValidator;
import com.agency8x.web.util.ControllerUtil;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

@Before(CheckLoginInterceptor.class)
public class WithdrawController extends Controller {

	// 获取和设置查询参数
	private SearchParamBean getAndSetQueryParams() {
		SearchParamBean bean = new SearchParamBean();
		bean.setWord(getPara("word", ""));
		bean.setDate1(getPara("date1", ""));
		bean.setDate2(getPara("date2", ""));
		this.setAttr("word", bean.getWord());
		this.setAttr("date1", bean.getDate1());
		this.setAttr("date2", bean.getDate2());
		return bean;
	}

	// 公共池
	public void index() {
		String word = this.getPara("word", "");// 搜索
		// 设置传入分页的url参数
		this.setAttr("append", "?word=" + word);
		// 选项卡
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);// 获取当前ID
		List<Record> tabList = AuthRuleModel.me.getRecordByPid(9, loginInfo.getUserId());
		this.setAttr("tabList", tabList);
		// 默认选项卡跳转
		if (!tabList.isEmpty()) {
			String tabName = tabList.get(0).getStr("rule_name");
			boolean jump = true;
			for (Record r : tabList) {
				if (r.getStr("rule_name").equals("public_list")) {
					jump = false;
				}
			}
			if (jump) {
				this.redirect("/withdraw/" + tabName);
				return;
			}
		}
		setAttr("listPage", WithdrawModel.me.paginate(getParaToInt(0, 1), 10, word));
		this.render("index.html");
	}

	// 我的待审核
	public void mypending_list() {
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);// 获取当前ID
		String word = this.getPara("word", "");// 搜索
		setAttr("listPage", WithdrawModel.me.paginatePending(getParaToInt(0, 1), 10, word, loginInfo.getUserId()));
		// 设置传入分页的url参数
		this.setAttr("append", "?word=" + word);
		this.setAttr("code", AdminModel.me.getOperatePassword(loginInfo.getUserId()));
		List<Record> tabList = AuthRuleModel.me.getRecordByPid(9, loginInfo.getUserId());
		this.setAttr("tabList", tabList);
		this.render("mypending_list.html");
	}

	// 待审核
	public void pending_list() {
		String word = this.getPara("word", "");// 搜索
		setAttr("listPage", WithdrawModel.me.paginatePending(getParaToInt(0, 1), 10, word, 0));
		// 设置传入分页的url参数
		this.setAttr("append", "?word=" + word);
		// 选项卡
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);// 获取当前ID
		List<Record> tabList = AuthRuleModel.me.getRecordByPid(9, loginInfo.getUserId());
		this.setAttr("tabList", tabList);
		this.render("pending_list.html");
	}

	// 审核记录(通过)
	public void pass_list() {
		SearchParamBean search = getAndSetQueryParams();
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);// 获取当前ID
		// 只能在Contoller中获取（需要this<Controller>）
		setAttr("listPage", WithdrawModel.me.paginateHistory(getParaToInt(0, 1), 10, search.getWord(),
				loginInfo.getUserId(), 1, search.getDate1(), search.getDate2()));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString());
		// 选项卡
		List<Record> tabList = AuthRuleModel.me.getRecordByPid(9, loginInfo.getUserId());
		this.setAttr("tabList", tabList);
		this.render("pass_list.html");
	}

	// 审核记录(驳回)
	public void rejected_list() {
		SearchParamBean search = getAndSetQueryParams();
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);// 获取当前ID
		// 只能在Contoller中获取（需要this<Controller>）
		String word = this.getPara("word", "");// 搜索
		String date1 = this.getPara("date1", "");// 起始时间
		String date2 = this.getPara("date2", "");// 终止时间
		this.setAttr("word", word);
		this.setAttr("date1", date1);
		this.setAttr("date2", date2);
		setAttr("listPage", WithdrawModel.me.paginateHistory(getParaToInt(0, 1), 10, search.getWord(),
				loginInfo.getUserId(), 2, search.getDate1(), search.getDate2()));
		// 设置传入分页的url参数
		this.setAttr("append", search.getQueryString());
		// 选项卡
		List<Record> tabList = AuthRuleModel.me.getRecordByPid(9, loginInfo.getUserId());
		this.setAttr("tabList", tabList);
		this.render("rejected_list.html");
	}

	public void audit() {
		Integer id = this.getParaToInt(0, 0);
		WithdrawModel model = null;
		if (id > 0) {
			model = WithdrawModel.me.getAuditRecord(id);
		}
		if (null == model) {
			ControllerUtil.renderDisptchJumpError(this, "记录不存在!");
			return;
		}
		this.setAttr("itemData", model);
		this.render("audit.html");
	}

	// 审核成功
	@Before(WithdrawValidator.class)
	public void doAudit() {
		ResultInfoBean resultBean;
		Integer status = this.getParaToInt("status");
		Integer id = this.getParaToInt("id", 0);
		String remark = this.getPara("remark", "");
		LoginModel loginBean = LoginModel.getLoginInfo(this, AppConf.loginSessionID);
		if (null == loginBean) {
			resultBean = new ResultInfoBean(false, "未登录或登录超时!");
			this.renderJson(resultBean);
			return;
		}
		resultBean = WithdrawModel.me.auditWithdraw(id, loginBean.getUserId(), status, remark);
		this.renderJson(resultBean);
	}

	// 撤销审核
	public void unAudit() {
		Integer id = this.getParaToInt();
		WithdrawModel.me.unAuditWithdraw(id);
	}

	/**
	 * 领取审核记录
	 */
	public void getRecord() {
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);
		Integer userId = loginInfo.getUserId();// 当前领取id
		String id = this.getPara();// 选择的数据id
		WithdrawModel.me.getRecord(userId, id);
		ControllerUtil.renderDisptchJumpSuccess(this, "领取成功!"); // 操作成功，倒计时3秒后自动刷新列表页面
		// String strAry[] = id.split(",");
		// for (int i = 0; i < strAry.length; i++) {}
		// this.render("index.html");
	}

	/**
	 * 报表导出
	 */
	public void doExportExcel() {
		LoginModel loginInfo = LoginModel.getLoginInfo(this, AppConf.loginSessionID);// 获取当前ID//
																						// 只能在Contoller中获取（需要this<Controller>）
		String word = this.getPara("word", "");// 搜索
		String date1 = this.getPara("date1", "");// 起始时间
		String date2 = this.getPara("date2", "");// 终止时间
		List<WithdrawModel> paginateHistory = WithdrawModel.me.paginateHistory1(getParaToInt(0, 1), 10, word,
				loginInfo.getUserId(), 1, date1, date2);
		// ComplexExportExcelClient.menu(paginateHistory);//导出
	}

	public void save() {
		this.renderText("save!!!");
	}
}
